﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Transactions;
using AB.ServiceDirectory.Entities;

namespace AB.ServiceDirectory.Dao
{
    public class ServiceDetail2Dao
    {
        private readonly ServicesDirectoryDataContext _context = DB.Context;

        //public IQueryable<ServiceReferenceData> LoadCategorybyId(int id)
        //{
        //    IQueryable<ServiceReferenceData> service = _context.ServiceReferenceDatas.Where(s => s.CategoryId == id);
        //    return service;
        //}

        public IQueryable<Intervention> SelectInterventions(int serviceId)
        {
            IQueryable<Intervention> interventions = from i in _context.Interventions
                                                     where (i.ServiceId == serviceId) || i.ServiceId == null
                                                     select i;
            return interventions;
        }

        //public IQueryable<Program> SelectPrograms()
        //{
        //    IQueryable<Program> query = from p in _context.Programs
        //                                select p;
        //    return query;
        //}

        public ServiceReference GetServiceById(int id)
        {
            return _context.ServiceReferences.FirstOrDefault(o => o.ServiceId == id);
        }

        public ServiceContract GetServiceContractById(int id)
        {
            return _context.ServiceContracts.FirstOrDefault(o => o.ServiceId == id);
        }

        public IQueryable<Service> GetOtherService(int serviceId)
        {
            Func<Service, Service> make = h => new Service {ServiceId = h.ServiceId, ServiceName = h.ServiceName};
            IQueryable<Service> services = from s in _context.Services
                                           where s.ServiceId != serviceId && s.IsActive
                                           select make(s);
            return services;
        }

        public bool EditIntervention(ICollection<Intervention> interventions)
        {
            using (var trans = new TransactionScope())
            {
                foreach (var item in interventions)
                {
                    var intervention = _context.Interventions.FirstOrDefault(i => i.InterventionId == item.InterventionId);
                    if (intervention != null)
                    {
                        intervention.ServiceId = item.ServiceId;
                    }
                }

                _context.SubmitChanges();
                trans.Complete();
                return true;
            }
        }

        public bool EditService2(Service service)
        {
            try
            {
                ServiceReference services = GetServiceById(service.ServiceId);
                ServiceContract contract = GetServiceContractById(service.ServiceId);
                if (services != null && contract != null)
                {
                    services.DisabilityCriterion = service.ServiceReference.DisabilityCriterion;
                    services.BarriersCriterion = service.ServiceReference.BarriersCriterion;
                    services.BenefitsCriterion = service.ServiceReference.BenefitsCriterion;
                    services.PersonalCircumstancesCriterion = service.ServiceReference.PersonalCircumstancesCriterion;
                    services.EthnicityCriterion = service.ServiceReference.EthnicityCriterion;
                    services.Accreditations = service.ServiceReference.Accreditations;
                    services.ClientJourney = service.ServiceReference.ClientJourney;
                    services.ClientOutcome = service.ServiceReference.ClientOutcome;
                    services.ParticipationCriterion = service.ServiceReference.ParticipationCriterion;
                    services.ClientSupportProcess = service.ServiceReference.ClientSupportProcess;
                    services.TargetClient = service.ServiceReference.TargetClient;
                    services.ReferralSources = service.ServiceReference.ReferralSources;
                    services.ProgramId = service.ServiceReference.ProgramId;
                    services.SupportCentres = service.ServiceReference.SupportCentres;

                    contract.ContractOutcome = service.ServiceContract.ContractOutcome;
                    contract.ContractObligation = service.ServiceContract.ContractObligation;
                    contract.Participation = service.ServiceContract.Participation;
                    _context.SubmitChanges();
                    return true;
                }
            }
            catch (SqlException exception)
            {
                Log.GetLog(this).Error(exception);
                return false;
            }
            return false;
        }

        public bool AddService2(Service newService)
        {
            try
            {
                if (newService != null)
                {
                    _context.Services.InsertOnSubmit(newService);
                    _context.SubmitChanges();
                    return true;
                }
            }
            catch (SqlException exception)
            {
                Log.GetLog(this).Error(exception);
                return false;
            }
            return false;
        }
    }
}